//
//  NSDate+CQDate.m
//  CQUtils
//
//  Created by runo on 16/5/17.
//  Copyright © 2016年 com.runo. All rights reserved.
//

#import "NSDate+CQDate.h"

@implementation NSDate (CQDate)

-(NSString *)cqToString{
    
    return  [self cqToStringWithFormatString:CQDataFromatType1];
}

-(NSString *)cqToStringWithFormatString:(CQDataFromatType)formatString{
    
    NSDateFormatter * formater=[[NSDateFormatter alloc] init];
    [formater setDateFormat:[NSDate cqFormatStringWithType:formatString]];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    [formater setTimeZone:localTimeZone];
    return [formater stringFromDate:self];
}

+(NSString *)cqNowString{
    
    return [[NSDate date] cqToString];
}

+(NSString *)cqNowDateToStringWithFormatString:(CQDataFromatType)formatString{
    
    return [[NSDate date] cqToStringWithFormatString:formatString];
}

+(NSDate *)cqGetDateFromString:(NSString *)string{
    
    return [NSDate cqGetDateWithFormatString:CQDataFromatType1 DateString:string];
    
}

+(NSDate *)cqGetDateWithFormatString:(CQDataFromatType)format DateString:(NSString *)dateString{
    
    NSDateFormatter * formater=[[NSDateFormatter alloc] init];
    [formater setDateFormat:[NSDate cqFormatStringWithType:format]];
    //得到的GMT时间
    NSDate *gmtDate = [formater dateFromString:dateString];
    return [NSDate cqGetNowDateFromatAnDate:gmtDate];
    
}

-(NSTimeInterval)cqTimeIntervalSinceNow{
    
    return [[NSDate cqGetNowDateFromatAnDate:[NSDate date]] timeIntervalSinceDate:self];
    
}

//获取当前时区时间
+ (NSDate *)cqGetNowDateFromatAnDate:(NSDate *)anyDate
{
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    //转为现在时间
    NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate] ;
    return destinationDateNow;
}

+(NSString *)cqGetHisvideoTime:(NSString *)fileName{
    
    NSString * sub=[fileName substringWithRange:NSMakeRange(8, 15)];
    sub=[sub stringByReplacingOccurrencesOfString:@"_" withString:@""];
    NSDate *date=[NSDate cqGetDateWithFormatString:CQDataFromatType2 DateString:sub];
    return [date cqToString];
    
    
}

+(NSString *)cqGetDateWithTimeIntervalSince1970:(NSTimeInterval)time Type:(CQDataFromatType)type{
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:[NSDate cqFormatStringWithType:type]];
    NSString *dateStr = [formatter stringFromDate:date];
    return dateStr;
    
}

+(NSTimeInterval )cqCaculateBeforTime:(NSString *)btime AfterTime:(NSString *)afterTime Type:(CQDataFromatType)type{
    
    NSDate *btimeDate = [NSDate cqGetDateWithFormatString:type DateString:btime];
    NSDate *afterDate = [NSDate cqGetDateWithFormatString:type DateString:afterTime];
    
    NSTimeInterval time = afterDate.timeIntervalSince1970 - btimeDate.timeIntervalSince1970;
    
    return time;
}

+(NSString *)cqIntervalString:(NSTimeInterval)interval{
    
    NSMutableString *intervalStr = [NSMutableString stringWithString:@""];
    
    long sec = (long)interval;
    long hour = sec/(60*60);
    long min = (sec - hour*(60*60))/60;
    if (hour > 0) {
        if (min>0) {
            hour += 1;
        }
        [intervalStr appendFormat:@"%ld小时",hour];
    }else if (min > 0) {
        [intervalStr appendFormat:@"%ld分钟",min];
    }else if (sec < 60) {
        [intervalStr appendFormat:@"%ld秒",sec];
    }
    return intervalStr;
    
}


/**
 *  根据类型获得对应的字符串
 *
 *  @param type 格式类型
 *
 *  @return 对应类型的字符串
 */
+(NSString *)cqFormatStringWithType:(CQDataFromatType)type{
    
    NSString *tmpString = nil;
    switch (type) {
        case CQDataFromatType1:
            tmpString = @"yyyy-MM-dd HH:mm:ss";
            break;
        case CQDataFromatType2:
            tmpString = @"yyyyMMddHHmmss";
            break;
        case CQDataFromatType3:
            tmpString = @"yyyy.MM.dd";
            break;
        case CQDataFromatType4:
            tmpString = @"yyyy-MM-dd";
            break;
        default:
            break;
    }
    
    return tmpString;
}

@end
